cmake_minimum_required(VERSION 2.6)
include(CheckIncludeFiles)
include(CheckCSourceCompiles)
include(CheckSymbolExists)

project(vine)
set(PROJECT_SOURCE_DIR src)
set(CFLAGS_ALL -Wall\ -g)
execute_process(COMMAND pkg-config --cflags gtk+-2.0 
		OUTPUT_VARIABLE GTK2_FLAGS
		OUTPUT_STRIP_TRAILING_WHITESPACE)
execute_process(COMMAND pkg-config --libs gtk+-2.0 
		OUTPUT_VARIABLE GTK2_LIBS
		OUTPUT_STRIP_TRAILING_WHITESPACE)
execute_process(COMMAND pkg-config --cflags python
       		OUTPUT_VARIABLE PY_CFLAGS
		OUTPUT_STRIP_TRAILING_WHITESPACE)	
#check build dependencies
check_c_source_compiles("int main(){__int64 test;}" USES_INT64)
check_c_source_compiles("int main(){return ({int a = 3; a+2;});}" BLOCK_EXPRESSION)
check_c_source_compiles("int main(){char* tmp = __FUNCTION__;}" HAVE_MACRO_FUNCTION)
check_c_source_compiles("int main(){char* tmp = __LINE__;}" HAVE_MACRO_LINE)
check_c_source_compiles("int main(){char* tmp = __FILE__;}" HAVE_MACRO_FILE)
set(CMAKE_REQUIRED_LIBRARIES "${GTK2_LIBS}")
set(CMAKE_REQUIRED_FLAGS "${GTK2_FLAGS}")
check_c_source_compiles("#include <gtk/gtk.h>\nint main(){g_thread_init(NULL);}" HAVE_G_THREAD_INIT)
set(CMAKE_REQUIRED_LIBRARIES)
set(CMAKE_REQUIRED_FLAGS)
check_include_files("malloc.h" HAVE_MALLOC_H)
check_include_files("limits.h" HAVE_LIMITS_H)
if(HAVE_MALLOC_H)
	check_symbol_exists(malloc "malloc.h" HAVE_MALLOC)
else()
	check_symbol_exists(malloc "stdlib.h" HAVE_MALLOC)
endif()
check_symbol_exists(fork "unistd.h" HAVE_FORK)

if(USES_INT64)
	set(LONG_LONG __int64)
else()
	set(LONG_LONG long\ long)
endif()
find_package(X11)
find_package(Threads)
find_package(PythonLibs)


#set include directories
include_directories("include")
include_directories("include/core")
include_directories("include/ui")
include_directories("include/pyloader")
include_directories("lib")
include_directories(".")

#set predefined macros
set(MAJOR_VERSION 0)
set(MINOR_VERSION 0)

#generate config.h
configure_file(config.h.in config.h)
add_definitions(-DHAVE_CONFIG_H -DLOG_LEVEL=6 -DDEFAULT_IMNAME=\"VINE\")

#lib IMdkit
aux_source_directory(lib IMdkit_FILES)
set_source_files_properties(${IMdkit_FILES} 
			    PROPERTIES
			    COMPILE_FLAGS  -fno-strength-reduce\ -fno-strict-aliasing\ ${CFLAGS_ALL}
			   )
add_library(IMdkit ${IMdkit_FILES})	
#######################################################################
aux_source_directory(ui ui_FILES)
set_source_files_properties(${ui_FILES}
			    PROPERTIES
			    COMPILE_FLAGS ${CFLAGS_ALL}\ ${GTK2_FLAGS}
			   )
add_library(ui STATIC ${ui_FILES})
set_target_properties(ui PROPERTIES LINK_FLAGS ${GTK2_LIBS})

###################################################################3
#main project
aux_source_directory(src vine_FILES)
set_source_files_properties(${vine_FILES}
			    PROPERTIES
			    COMPILE_FLAGS ${CFLAGS_ALL}
			   )
add_executable(vine ${vine_FILES})
target_link_libraries(vine X11)
target_link_libraries(vine IMdkit)
target_link_libraries(vine ${CMAKE_THREAD_LIBS_INIT})
target_link_libraries(vine ui)
target_link_libraries(vine ${GTK2_LIBS})
#set_target_properties(vine PROPERTIES LINK_FLAGS ${GTK2_LIBS})

#####################################################################

macro(add_input_method basedir imname cflags ldflags)
	set(__sources__ )
	aux_source_directory(${basedir}/${imname}/ __sources__)
	set_source_files_properties(${__sources__}
				    PROPERTIES
				    COMPILE_FLAGS ${cflags}\ ${CFLAGS_ALL})
	add_library(${imname} SHARED ${__sources__})
	set_target_properties(${imname} 
			      PROPERTIES
			      LINK_FLAGS ${ldflags})
endmacro(add_input_method)

#Build IME Here

add_input_method(ime sample -g\ -DNAME=2 -fPIC)
add_input_method(ime english -g -fPIC)
add_input_method(ime pyloader -g\ ${PY_CFLAGS} -fPIC)
target_link_libraries(pyloader ${PYTHON_LIBRARIES})
